package com.xx.news.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xx.news.BR;
import com.xx.news.R;
import com.xx.news.adapter.QicheAdapter;
import com.xx.news.adapter.XinwenAdapter;
import com.xx.news.base.BaseActivity;
import com.xx.news.base.BaseFragment;
import com.xx.news.databinding.ActivityQicheBinding;
import com.xx.news.databinding.ActivityXinwenBinding;
import com.xx.news.entity.Qiche;
import com.xx.news.utils.MyDividerItemDecoration;
import com.xx.news.viewmodel.QicheModel;
import com.xx.news.viewmodel.XinwenViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * mvvm
 */
public class XinwenActivity extends BaseFragment<ActivityXinwenBinding, XinwenViewModel> {

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.activity_xinwen;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    private RecyclerView mRecyclerView;
    private static XinwenAdapter mAdapter;
    private static List<Qiche.ListBean> mList;

    @Override
    public void initData() {
        super.initData();
        viewModel.getDate();

        mRecyclerView = binding.recycler;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        MyDividerItemDecoration decor = new MyDividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(decor);
        mList = new ArrayList<>();
        mAdapter = new XinwenAdapter(mList, getActivity());
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.addItemClickListener(new XinwenAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Qiche.ListBean item) {
                Intent intent = new Intent(getActivity(), QicheInfoActivity.class);
                intent.putExtra("bean", item);
                startActivity(intent);
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.getLiveData().observe(this, new Observer<Qiche>() {
            @Override
            public void onChanged(Qiche qiche) {
                //这里更新UI
                mAdapter.updateData(qiche.getList());
            }
        });
    }
}